using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

public class FrameAnimationController : MonoBehaviour
{
    [Header("帧动画设置")]
    public List<Sprite> animationFrames = new List<Sprite>();
    public Image targetImage;
    public bool loopAnimation = true;
    public bool autoPlay = false;
    public float autoPlaySpeed = 0.1f;
    [Header("调试信息")]
    public int currentFrameIndex = 0;
    
    private bool isAutoPlaying = false;
    
    void Start()
    {
        if (targetImage == null)
        {
            targetImage = GetComponent<Image>();
        }
        
        if (targetImage == null)
        {
            targetImage = GetComponentInChildren<Image>();
        }
        
        if (targetImage == null)
        {
            Debug.LogError("FrameAnimationController: 找不到目标Image组件！");
            return;
        }
        
        // 设置初始帧
        if (animationFrames.Count > 0)
        {
            SetFrame(0);
        }
        
        // 如果启用自动播放
        if (autoPlay)
        {
            StartAutoPlay();
        }
    }

    public void OnButtonClick()
    {
        NextFrame();
    }
    /// <summary>
    /// 切换到下一帧
    /// </summary>
    public void NextFrame()
    {
        if (animationFrames.Count == 0)
        {
            Debug.LogWarning("FrameAnimationController: 没有动画帧！");
            return;
        }
        
        currentFrameIndex++;
        
        // 处理循环
        if (currentFrameIndex >= animationFrames.Count)
        {
            if (loopAnimation)
            {
                currentFrameIndex = 0;
            }
            else
            {
                currentFrameIndex = animationFrames.Count - 1;
                return;
            }
        }
        
        SetFrame(currentFrameIndex);
        Debug.Log($"切换到第 {currentFrameIndex + 1} 帧");
    }
    
    /// <summary>
    /// 切换到上一帧
    /// </summary>
    public void PreviousFrame()
    {
        if (animationFrames.Count == 0) return;
        
        currentFrameIndex--;
        
        if (currentFrameIndex < 0)
        {
            if (loopAnimation)
            {
                currentFrameIndex = animationFrames.Count - 1;
            }
            else
            {
                currentFrameIndex = 0;
                return;
            }
        }
        
        SetFrame(currentFrameIndex);
        Debug.Log($"切换到第 {currentFrameIndex + 1} 帧");
    }
    
    /// <summary>
    /// 设置指定帧
    /// </summary>
    /// <param name="frameIndex">帧索引</param>
    public void SetFrame(int frameIndex)
    {
        if (animationFrames.Count == 0 || targetImage == null) return;
        
        frameIndex = Mathf.Clamp(frameIndex, 0, animationFrames.Count - 1);
        currentFrameIndex = frameIndex;
        
        if (animationFrames[frameIndex] != null)
        {
            targetImage.sprite = animationFrames[frameIndex];
        }
    }
    
    /// <summary>
    /// 重置到第一帧
    /// </summary>
    public void ResetToFirstFrame()
    {
        SetFrame(0);
    }
    
    /// <summary>
    /// 开始自动播放
    /// </summary>
    public void StartAutoPlay()
    {
        if (!isAutoPlaying)
        {
            isAutoPlaying = true;
            InvokeRepeating(nameof(NextFrame), autoPlaySpeed, autoPlaySpeed);
        }
    }
    
    /// <summary>
    /// 停止自动播放
    /// </summary>
    public void StopAutoPlay()
    {
        if (isAutoPlaying)
        {
            isAutoPlaying = false;
            CancelInvoke(nameof(NextFrame));
        }
    }
    
    /// <summary>
    /// 切换自动播放状态
    /// </summary>
    public void ToggleAutoPlay()
    {
        if (isAutoPlaying)
        {
            StopAutoPlay();
        }
        else
        {
            StartAutoPlay();
        }
    }
    
    /// <summary>
    /// 添加动画帧
    /// </summary>
    /// <param name="sprite">要添加的精灵</param>
    public void AddFrame(Sprite sprite)
    {
        if (sprite != null)
        {
            animationFrames.Add(sprite);
        }
    }
    
    /// <summary>
    /// 清空所有帧
    /// </summary>
    public void ClearFrames()
    {
        animationFrames.Clear();
        currentFrameIndex = 0;
    }
    
    /// <summary>
    /// 获取当前帧信息
    /// </summary>
    /// <returns>当前帧信息字符串</returns>
    public string GetFrameInfo()
    {
        return $"第 {currentFrameIndex + 1} 帧 / 共 {animationFrames.Count} 帧";
    }
}